<?php
class indexControl extends Control{
	/**
	 * 网站首页
	 */
    public function index(){
    	$catid = 68;
    	$cat = M("category");
    	$cats = $cat->all();
    	foreach($cats as $k=>$v){ //获取当前栏目的信息
    		if($v['catid'] == $catid){
    			$self_cat = $cats[$k]; //当前栏目信息
    		}
    	}
    	$self_pid = $self_cat['pid'];
    	$slef_cat = ArrToCat($cats, $self_pid);
    	$self_cats = channleToArr($slef_cat);
    	$html  = catArr2html($self_cats);
    	$this->assignHeader();
    	$this->display();
    }
    /**
     * 文章页
     */
    public function article(){
    	$mid = isset($_GET['mid'])?$_GET['mid']:false; //模型ID
    	if($mid === false){
    		$this->error('参数错误');
    	}
    	$nid = isset($_GET['nid'])?$_GET['nid']:false; //文章ID
    	if($nid === false){
    		$this->error('参数错误');
    	}
    	$tables = $this->getAllTableName($mid);
    	$ptable = $tables['p']; //主表无前缀    //文章所在的主表
    	$stable = $tables['s']; //附表无前缀 // 文章所在的附表
    	$news = V($ptable);
    	$news->view = array(
    			$stable => array(
    					'join_type'=>'inner',
    					'on'=>$ptable.'.id = '.$stable.'.nid',
    				),
    			'category' => array(
    					'join_type'=>'inner',
    					'field'=>'cname,mid,contpl,pid',
    					'on'=>$ptable.'.catid = category.catid',
    				),
    			'user'=>array(
    					'join_type'=>'inner',
    					'on'=>$ptable.'.uid = user.uid',
    				),
    		);
    	$row = $news->find($nid);
    	$contpl = $row['contpl'];
    	if(empty($row)){
    		$this->error('参数错误');
    	}
    	$this->assignHeader();
    	$this->assign('new', $row);
    	$this->display($contpl);
    }
    /**
     * AJAX记录文章的访问次数
     */
    public function pageClickNum(){
    	c('debug',0);
    	$mid = isset($_POST['mid'])?$_POST['mid']:false; //模型ID
    	$nid = isset($_POST['nid'])?$_POST['nid']:false; //文章ID
    	$tables = $this->getAllTableName($mid);
    	$ptable = $tables['p']; //主表无前缀    //文章所在的主表
    	$news = V($ptable);
    	if($nid){
    		$news->inc('click','id = '.$nid,1);
    	}
    }
    /**
     * 列表页
     */
    public function catlist(){
    	$mid = isset($_GET['mid'])?$_GET['mid']:false; //模型ID
    	if($mid === false){
    		$this->error('参数错误');
    	}
    	$catid = isset($_GET['catid'])?$_GET['catid']:false; //栏目ID
    	if($catid === false){
    		$this->error('参数错误');
    	}
    	$tables = $this->getAllTableName($mid);
    	$ptable = $tables['p']; //主表无前缀    
    	$news =  M($ptable);
    	
    	$this->assignHeader();
    	$this->display();
    }
	
    /**
     * 获得主副表名
     * @param number $mid  //模型id
     * @return Array  //主副表数组
     *  0为主表   1为附表
     */
    public function getAllTableName($mid){
    	$prefix = C('DB_PREFIX');//表前缀
    	$model = M('model');
    	$model_row = $model->find($mid);
    	$table['p'] = $model_row['ptable'];//(主表) 无前缀
    	$table['s'] = isset($model_row['stable'])?$model_row['stable']:'';//(附表) 无前缀
    	$table['pfix'] = $prefix.$table['p'];//(主表) 有前缀
    	$table['sfix'] = $prefix.$table['s']; //(附表) 有前缀
    	return $table;
    }
    /**
     * 发送栏目信息到文章页面
     */
	public function assignHeader(){
		$cat = M("category");
		$category = $cat->field("catid,cname,sort,pid,mid")->all();
		if(empty($category)){
			$category = array();
		}
	
		$category = arr::channel($category,"catid","pid",0,0,2,"&nbsp;");
		$category = channleToArr($category);
		$category = createCatList($category,'level');
		$this->assign("category",$category);
	}
	/**
	 * 处理评论
	 */
	public function comment(){
		C('debug',0);
		P($_POST);
		P(json_encode($_POST));
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
}